package com.example.service.wechat.impl;

import com.example.mapper.web.BikeMapper;
import com.example.mapper.wechat.OrderMapper;
import com.example.pojo.web.Bike;
import com.example.pojo.web.Result;
import com.example.pojo.wechat.OpenId_SessionKey;
import com.example.pojo.wechat.Order;
import com.example.service.wechat.OrderService;
import com.example.service.wechat.WxAuthenticService;
import com.example.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName OrderServiceImpl
 * @Description: com.example.service.wechat.impl
 * @Auther: xiwd
 * @Date: 2022/4/19 - 04 - 19 - 0:22
 * @version: 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    //properties
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private BikeMapper bikeMapper;
    @Autowired
    private WxAuthenticService wxAuthenticService;

    //methods
    @Override
    public Order selectOrderByOpenId_Status(String openid, Integer status) {
        try {
            return orderMapper.selectOrderByOpenId_Status(openid, status);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Order selectOrderById(Integer id) {
        try {
            return orderMapper.selectOrderById(id);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result selectOrderByOpenId(String code) {
        try{
            //根据code获取openid
            OpenId_SessionKey openId_sessionKey = wxAuthenticService.getOpenId_SessionKey(code);
            if (openId_sessionKey==null)
                return ResultUtil.error(-1,"查询失败");
            String openid = openId_sessionKey.getOpenid();
            //查询订单
            List<Order> orders = orderMapper.selectOrderByOpenId(openid);
            return ResultUtil.success(1,"查询成功",orders);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Result updateOrderStatusById(Integer id, Integer status) {
        try {
            //获取订单
            Order order = this.selectOrderById(id);
            if (order==null){
                return  ResultUtil.error(0,"结束订单失败");
            }
            //获取单车编号
            Integer markerid = order.getMarkerid();
            //更新单车状态
            Integer rows = bikeMapper.updateBikeStatusByID(markerid, 0);
            if (rows>0){
                Integer rows_order = orderMapper.updateOrderStatusById(id, status);
                if (rows_order>0){
                    return  ResultUtil.error(1,"结束订单成功");
                }else {
                    return  ResultUtil.error(0,"结束订单失败");
                }
            }else {
                return  ResultUtil.error(0,"结束订单失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return  ResultUtil.error(1,"系统异常");
        }
    }

    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    @Override
    public Result addOrder(String code, Integer markerId) {
        try {
            //获取单车状态
            Bike bike = bikeMapper.selectBikeById(markerId);
            if (bike!=null&&bike.getStatus()!=0)
                return  ResultUtil.error(-1,"创建订单失败");
            //根据code获取openid
            OpenId_SessionKey openId_sessionKey = wxAuthenticService.getOpenId_SessionKey(code);
            if (openId_sessionKey==null)
                return ResultUtil.error(-1,"创建订单失败");
            String openid = openId_sessionKey.getOpenid();
            //查询是否存在未完成的订单
            Order order = this.selectOrderByOpenId_Status(openid, 0);
            if (order!=null)
                return ResultUtil.error(0,"存在尚未完成的订单");
            //增加订单
            Integer rows = orderMapper.addOrder(openid, markerId);
            if (rows>0){
                //修改单车状态
                Integer rowsBike = bikeMapper.updateBikeStatusByID(markerId, 1);
                if (rowsBike>0){
                    return ResultUtil.success(1,"添加订单成功");
                }else {
                    return ResultUtil.error(-1,"创建订单失败");
                }
            }else {
                return ResultUtil.error(-1,"创建订单失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(-1,"创建订单失败");
        }
    }
}
